<?php
/**
 * Created by PhpStorm.
 * User: jian
 * Date: 18-8-2
 * Time: 下午12:33
 */

namespace app\api\controller;

use think\Db;
use think\Request;
use think\Session;

class User extends Conmon
{
    /********** 登录 *********/
    public function login ()
    {
        $request = Request::instance();
        $data    = $request->param();
        $user    = Db::table('sy_user')
            ->field('user_id,user_name,user_email,user_rtime')
            ->where('user_name', 'eq', $data['user_name'])
            ->where('user_pwd', 'eq', md5($data['user_pwd']))
            ->find();
        if ($user) {
            $token = Session::get('token');
            header('Authorization:' . $token);
            $this->return_msg(200, '登录成功', $user);
        } else {
            $this->return_msg(400, '登录失败!账号或密码错误～');
        }
    }

    public function index ($id)
    {
        $user = Db::table('sy_user')
            // ->field('user_id,user_name,user_email,user_etime')
            ->field('user_id,user_name,user_email,user_rtime')
            ->where('user_id', 'eq', $id)
            ->find();
        if ($user) {
            $this->return_msg(200, '获取成功', $user);
        }
        $this->return_msg(400, '获取失败');
    }

    public function up_pwd ()
    {
        $request  = Request::instance();
        $id       = $request->param('id');
        $password = md5($request->param('password'));
        $user     = Db::table('sy_user')
            ->where('user_id', 'eq', $id)
            ->update(['user_pwd' => $password]);
        if ($user) {
            $this->return_msg(200, '修改成功');
        }
        $this->return_msg(400, '修改失败');
    }

    public function up_email ()
    {
        $request = Request::instance();
        $id      = $request->param('id');
        $email   = $request->param('email');
        $user    = Db::table('sy_user')
            ->where('user_id', 'eq', $id)
            ->update(['user_email' => $email]);
        if ($user) {
            $this->return_msg(200, '修改成功');
        }
        $this->return_msg(400, '修改失败');
    }
}
